/*
 * $Id: TaskNResource.java,v 1.8 2009/10/30 22:29:19 navdeep_mahajan Exp $
 *
 * Copyright (C) 2006 General Electric Company. All Rights Reserved.
 *
 * This software is the confidential and proprietary information of the General
 * Electric Company (GE). You shall not disclose this software and shall use it
 * only in accordance with the terms of the license agreement you entered into
 * with GE.
 *
 * GE MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
 * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
 * NON-INFRINGEMENT. GE SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
 * AS A RESULT OF USING, MODIFYING, OR DISTRIBUTING THIS SOFTWARE OR ITS
 * DERIVATIVES.
 */

package com.ge.healthcare.autosc.common.pojo;

import java.io.Serializable;
import java.util.Date;

import org.apache.regexp.RESyntaxException;

import com.ge.healthcare.autosc.common.ASCUtil;
import com.ge.healthcare.autosc.common.database.pojo.ActTask;
import com.ge.healthcare.autosc.common.database.pojo.IActTask;
import com.ge.healthcare.autosc.common.util.regexp.SRE;

public class TaskNResource implements IActTask, Serializable {
	/**
	 * Eclipse generated
	 */
	private static final long serialVersionUID = 6293403659297710840L;
	public static final String DEFAULT = "DEFAULT";
	private Long priority;
	private String category;
	private Long taskId;
	private String portMaster;
	private String serviceCenter;
	private String phoneNumber;
	private ActTask actTask;

	private SRE allEight = null;
	private SRE allNine = null;

	public TaskNResource() {
		try {
			allEight = new SRE("^\\s*8+\\s*", SRE.MATCH_CASEINDEPENDENT);
			allNine = new SRE("^\\s*9+\\s*", SRE.MATCH_CASEINDEPENDENT);
		} catch (RESyntaxException e) {
			StringBuffer sb = new StringBuffer();
			sb
					.append("TaskNResource failed to initialize Regualar expression: ");
			sb.append(e.getMessage());
			new RuntimeException(sb.toString());
		}
	}

	/**
	 * @return the portMaster
	 */
	public String getPortMaster() {
		return portMaster;
	}

	/**
	 * @param portMaster
	 *            the portMaster to set
	 */
	public void setPortMaster(String portMaster) {
		this.portMaster = portMaster;
	}

	/**
	 * @return the serviceCenter
	 */
	public String getServiceCenter() {
		return serviceCenter;
	}

	/**
	 * @param serviceCenter
	 *            the serviceCenter to set
	 */
	public void setServiceCenter(String serviceCenter) {
		this.serviceCenter = serviceCenter;
	}

	/**
	 * @return the phoneNumber
	 */
	public String getPhoneNumber() {
		return phoneNumber;
	}

	/**
	 * @param phoneNumber
	 *            the phoneNumber to set
	 */
	public void setPhoneNumber(String phoneNumber) {
		this.phoneNumber = phoneNumber;
	}

	public TaskNResource(Long taskId) {
		this.taskId = taskId;
	}

	/**
	 * @return the taskId
	 */
	public Long getTaskId() {
		return taskId;
	}

	/**
	 * @param taskId
	 *            the taskId to set
	 */
	public void setTaskId(Long taskId) {
		this.taskId = taskId;
	}

	/**
	 * @return the priority
	 */
	public Long getPriority() {
		if (ASCUtil.isNullOrEmptyObject(priority)) {
			return 5L;
		} else {
			return priority;
		}
	}

	/**
	 * @param priority
	 *            the priority to set
	 */
	public void setPriority(Long priority) {
		this.priority = priority;
	}

	/**
	 * @return the category
	 */
	public String getCategory() {
		if (ASCUtil.isNullOrEmpty(category)) {
			return DEFAULT;
		} else {
			return category;
		}
	}

	/**
	 * @param category
	 *            the category to set
	 */
	public void setCategory(String category) {
		this.category = category;
	}

	public boolean isDialUpSystem() {
		boolean isDialUp = false;
		if (!ASCUtil.isNullOrEmpty(portMaster)) {
			// If the phone is not all 8s or 9s
			if (!allEight.match(this.phoneNumber)
					&& !allNine.match(this.phoneNumber)) {
				isDialUp = true;
			}
		}
		return isDialUp;
	}

	public void setPhoneNumber(Object ext, Object number) {
		StringBuffer phone = new StringBuffer();
		if (!ASCUtil.isNullOrEmptyObject(ext)) {
			phone.append(ext);
		}
		if (!ASCUtil.isNullOrEmptyObject(number)) {
			phone.append(number);
		}
		this.phoneNumber = phone.toString();
	}

	/**
	 * @return the actTask
	 */
	public ActTask getActTask() {
		return actTask;
	}

	/**
	 * @param actTask
	 *            the actTask to set
	 */
	public void setActTask(ActTask actTask) {
		this.actTask = actTask;
	}

	public boolean equals(TaskNResource task) {
		boolean isEqual = false;
		if (this.getTaskId().equals(task.getTaskId())) {
			isEqual = true;
		}
		return isEqual;
	}

	public String toString() {
		StringBuffer msg = new StringBuffer();
		msg.append("{priority=").append(this.getPriority()).append("},");
		msg.append("{category=").append(this.getCategory()).append("},");
		msg.append("{taskId=").append(this.getTaskId()).append("},");
		msg.append("{portMaster=").append(this.getPortMaster()).append("},");
		msg.append("{serviceCenter=").append(this.getServiceCenter()).append(
				"},");
		msg.append("{phoneNumber=").append(this.getPhoneNumber()).append("},");
		msg.append("{actTask=").append(this.getActTask()).append("},");
		return msg.toString();
	}
	
	public void setQueuedTime(Date queuedTime){
		if(!ASCUtil.isNullOrEmptyObject(this.getActTask())){
			this.getActTask().setQueuedTime(queuedTime);
		}
	}

	public TaskNResource copy() {
		TaskNResource tnr = new TaskNResource();
		if (!ASCUtil.isNullOrEmptyObject(priority)) {
			tnr.setPriority(new Long(priority));
		}
		if (!ASCUtil.isNullOrEmptyObject(category)) {
			tnr.setCategory(new String(category));
		}
		if (!ASCUtil.isNullOrEmptyObject(taskId)) {
			tnr.setTaskId(new Long(taskId));
		}
		if (!ASCUtil.isNullOrEmptyObject(portMaster)) {
			tnr.setPortMaster(new String(portMaster));
		}
		if (!ASCUtil.isNullOrEmptyObject(serviceCenter)) {
			tnr.setServiceCenter(new String(serviceCenter));
		}
		if (!ASCUtil.isNullOrEmptyObject(phoneNumber)) {
			tnr.setPhoneNumber(new String(phoneNumber));
		}
		if (!ASCUtil.isNullOrEmptyObject(actTask)) {
			tnr.setActTask(actTask.copy());
		}
		return tnr;
	}
}
